import { toChartData } from "@/comm/format";
import { Bus, CHART_KLINE__ITEM, CHART_KLINE__LIST, CONN_SIGNAL } from "../connetion/Bus";
import { IBroadcast } from "./IBroadcast";

export class SignalBroadcast extends IBroadcast {
    channelParam: any;
    symbol = "";
    interval = "";
    channelStr = "";

    changeParam(symbol: string, interval: string) {
        this.symbol = symbol;
        if (this.interval == "") {
            this.interval = interval;
        }
        this.channelParam = {
            event: "subscribe",
            group: symbol,
            topic: "SIGNAL"
        };
        this.subscribe();
    }

    changeSymbol(symbol: string) {
        this.symbol = symbol;
        this.channelParam.symbol = symbol;
        this.subscribe();
    }

    changeInterval(interval: string) {
        this.interval = interval;
        this.channelParam.interval = interval;
        this.subscribe();
    }

    subscribe(): void {
        this.unSubscribe();
        if (!this.channelParam) {
            // console.log("订阅参数未初始化");
            return;
        }

        this.channelStr = JSON.stringify(this.channelParam);
        // console.log("###订阅", this.channelStr);
        this.send(this.channelStr);
    }

    unSubscribe(): void {
        // console.warn("取消订阅 kline 开始")
        if (!this.channelStr) return;

        const obj = JSON.parse(this.channelStr);
        obj.event = "unsubscribe";
        // console.log("###取消订阅", JSON.stringify(obj));

        this.send(JSON.stringify(obj));
    }

    handle(jsondata: any): void {
        console.log("信号", jsondata);
        Bus.emit(CONN_SIGNAL, jsondata);
    }
}
